package com.zzyl.nursing.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.vo.AlertDataVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 报警数据Controller
 *
 * @author syl
 * @date 2025-06-05
 */
@RestController
@RequestMapping("/nursing/alertData")
@Api(tags = "报警数据的接口")
public class AlertDataController extends BaseController {
    @Autowired
    private IAlertDataService alertDataService;

    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据列表
     */
    @ApiOperation("查询报警数据列表")
//    @PreAuthorize("@ss.hasPermi('nursing:alertData:list')")
    @GetMapping("/list")
    public TableDataInfo list(AlertDataVo alertDataVo) {
        startPage();
        List<AlertData> list = alertDataService.selectAlertDataList(alertDataVo);
        return getDataTable(list);
    }

    /**
     * 导出报警数据列表
     */
   /* @ApiOperation("导出报警数据列表")
    @PreAuthorize("@ss.hasPermi('nursing:alertData:export')")
    @Log(title = "报警数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlertData alertData) {
        List<AlertData> list = alertDataService.selectAlertDataList(alertData, careUserId);
        ExcelUtil<AlertData> util = new ExcelUtil<AlertData>(AlertData.class);
        util.exportExcel(response, list, "报警数据数据");
    }*/

    /**
     * 获取报警数据详细信息
     */
    @ApiOperation("获取报警数据详细信息")
    @PreAuthorize("@ss.hasPermi('nursing:alertData:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "报警数据ID", required = true)
                              @PathVariable("id") Long id) {
        return success(alertDataService.selectAlertDataById(id));
    }

    /**
     * 新增报警数据
     */
    @ApiOperation("新增报警数据")
    @PreAuthorize("@ss.hasPermi('nursing:alertData:add')")
    @Log(title = "报警数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam(value = "报警数据实体")
                          @RequestBody AlertData alertData) {
        return toAjax(alertDataService.insertAlertData(alertData));
    }

    /**
     * 修改报警数据
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("修改报警数据")
    @PutMapping("/handleAlertData")
//    @PreAuthorize("@ss.hasPermi('nursing:alertData:edit')")
    @Log(title = "报警数据", businessType = BusinessType.UPDATE)
    public AjaxResult handleAlertData(@ApiParam(value = "报警数据实体")
                           @RequestBody AlertDataDto alertDataDto) {
        return toAjax(alertDataService.updateAlertData(alertDataDto));
    }

    /**
     * 删除报警数据
     */
    @ApiOperation("删除报警数据")
    @PreAuthorize("@ss.hasPermi('nursing:alertData:remove')")
    @Log(title = "报警数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(alertDataService.deleteAlertDataByIds(ids));
    }

    /**
     *
     */
/*
    @PutMapping("/handleAlertData")
//    @PreAuthorize("@ss.hasPermi('nursing:alertData:handleAlertData')")
    @Log(title = "报警数据", businessType = BusinessType.UPDATE)
    public AjaxResult handleAlertData(@ApiParam(value = "报警数据实体")
                                      @RequestBody AlertData alertData) {
        //获取当前用户ID
        Long userId = SecurityUtils.getUserId();
        String userName = SecurityUtils.getUsername();
        //先检查该老人护理员是否有权处理该报警数据
        AlertData alertDataById = alertDataService.selectAlertDataById(alertData.getId());
        if(alertDataById==null||!isCareUser(userId,alertDataById.getUserId())){
            return error("无权处理该报警数据");
        }

        alertDataService.handleAlertData(alertData.getId(), alertData.getProcessingResult(), userId, userName);
        return success();
    }
    */
/**
     * 检查护理员是否负责该老人
     *//*

    private boolean isCareUser(Long userId,Long careUserId){
        Integer count = alertDataMapper.checkCareElderById(userId,careUserId);
        return count != null && count > 0;
    }
*/

}
